// 处理业务逻辑的中间件,读取某个json文件的数据
const path = require("path");
const fileUtils = require("../utils/file_utils");
module.exports = async (ctx, next) => {
	// 根据url
	const url = ctx.request.url; //
	console.log("1", url);
	let filePath = url.replace("/api", "");
	filePath = juadePathRouteJson(filePath);
	console.log("2", filePath);
	filePath = "../data" + filePath + ".json";
	console.log("3", filePath);
	filePath = path.join(__dirname, filePath);
	console.log("4", filePath);
	try {
		const ret = await fileUtils.getFileJsonData(filePath);
		console.log("5", ret);
		ctx.response.body = ret;
	} catch (error) {
		const errorMsg = {
			msg: "读取文件内容失败, 文件资源不存在",
			code: 404,
		};
		ctx.response.body = JSON.stringify(errorMsg);
	}

	console.log(filePath);
	await next();
};

const juadePathRouteJson = (filePath) => {
	let copyFilePath = filePath;
	if (filePath.includes("/permssion/v1.0.0/user/uipermssion/13300000000")) {
		copyFilePath = `/permssion/index`;
	}
	if (filePath.includes("/permssion/v1.0.0/user/uipermssion/menus")) {
		copyFilePath = `/permssion/menus`;
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/failed/report/queryPage")
	) {
		copyFilePath = `/inneral/failedReport/index`;
	}

	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/trafficalarm/captureRecordQueryPage?pageNum=1&pageSize=20&licensePlateNumber="
		)
	) {
		if (filePath.includes("%E6%97%A0%E8%BD%A6%E7%89%8C")) {
			copyFilePath = `/inneral/failedReport/captureRecord`;
		} else if (filePath.includes("%E9%99%95ABY676")) {
			copyFilePath = `/inneral/followPath/captureRecord1`;
		} else if (filePath.includes("%E9%99%95ABD533")) {
			copyFilePath = `/inneral/followPath/captureRecord`;
		} else if (filePath.includes("%E5%B7%9DH33423")) {
			copyFilePath = `/inneral/suspend/captureRecord`;
		} else if (filePath.includes("%E9%99%95KH9068")) {
			copyFilePath = `/inneral/frequently/captureRecord`;
		} else {
			copyFilePath = `/inneral/followPath/captureRecord`;
		}
	}
	if (filePath.includes("/permssion/v1.0.0/user/param?userId=1&type=")) {
		if (filePath.includes("ztcglsb&key=ztcglsb_521343769212103120")) {
			copyFilePath = `/permssion/param`;
		} else if (filePath.includes("ztcglsb&key=ztcglsb_521343769337932642")) {
			copyFilePath = `/permssion/param`;
		} else {
			copyFilePath = `/permssion/param`;
		}
	}
	if (filePath.includes("/device/v1.0.0/dev/condintionSearch")) {
		copyFilePath = `/device/dev/search`;
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/follow/route/queryPage")
	) {
		copyFilePath = `/inneral/followPath/index`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/car/queryPage?pageNum=1&pageSize=20&plateNum="
		)
	) {
		if (filePath.includes("%E6%97%A0%E8%BD%A6%E7%89%8C")) {
			copyFilePath = `/inneral/failedReport/noCar`;
		} else if (filePath.includes("%E9%99%95ABY676")) {
			copyFilePath = `/inneral/followPath/lat1`;
		} else if (filePath.includes("%E9%99%95ABY676")) {
			copyFilePath = `/inneral/blackalarm/plateNum`;
		} else if (filePath.includes("%E5%B7%9DH33423")) {
			copyFilePath = `/inneral/blackalarm/plateNum`;
		} else {
			copyFilePath = `/inneral/followPath/lat1`;
		}
	}

	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/trafficalarm/getProcessHistoryDetail?licensePlateNumber="
		)
	) {
		if (filePath.includes("%E9%99%95ABY676")) {
			copyFilePath = `/inneral/followPath/1`;
		} else if (filePath.includes("%E9%99%95ABD533")) {
			copyFilePath = `/inneral/followPath/historyDetail`;
		} else {
			copyFilePath = `/inneral/followPath/historyDetail`;
		}
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/everyday/queryPage?licensePlateNumber="
		)
	) {
		if (filePath.includes("%E9%99%95ABY676")) {
			copyFilePath = `/inneral/followPath/everyday1`;
		} else if (filePath.includes("%E9%99%95ABD533")) {
			copyFilePath = `/inneral/followPath/everyday`;
		} else {
			copyFilePath = `/inneral/followPath/everyday`;
		}
	}

	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/qinhanroute/getKKXXByRoadName?roadName"
		)
	) {
		copyFilePath = `/inneral/followPath/roadName`;
	}
	if (filePath.includes("/inneral-muck-analyze/v1.0.0/blackalarm/queryPage")) {
		copyFilePath = `/inneral/blackalarm/index`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/car/queryPage?pageNum=1&pageSize=20&plateNum="
		)
	) {
		if (filePath.includes("%E9%99%95U2755X")) {
			copyFilePath = `/inneral/blackalarm/plateNum`;
		}
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/trafficalarm/captureRecordQueryPage?pageNum=1&pageSize=20"
		)
	) {
		if (filePath.includes("%E9%99%95U2755X")) {
			copyFilePath = `/inneral/blackalarm/captureRecord`;
		}
	}
	if (
		filePath.includes("/permssion/v1.0.0/user/param?userId=1&type=ztcglsb&key=")
	) {
		if (filePath.includes("ztcglsb_521343769207908807")) {
			copyFilePath = `/inneral/blackalarm/keylist1`;
		}
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/trafficalarm/getAllKKXX")
	) {
		copyFilePath = `/inneral/blackalarm/allKKXX`;
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/suspend/dump/queryPage")
	) {
		copyFilePath = `/inneral/suspend/index`;
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/trafficalarm/statistics")
	) {
		copyFilePath = `statistics`;
	}
	if (filePath.includes("/inneral-muck-analyze/v1.0.0/electronicfence/page")) {
		copyFilePath = `/electronicfence/index`;
	}
	if (filePath.includes("/inneral-muck-analyze/v1.0.0/electronicfence/info")) {
		if (filePath.includes("2c92808d833a95ac01833aabecd1002d")) {
			copyFilePath = `/electronicfence/1`;
		} else if (filePath.includes("7201831bea9014001d")) {
			copyFilePath = `/electronicfence/2`;
		} else {
			copyFilePath = `/electronicfence/1`;
		}
	}
	if (
		filePath.includes("/inneral-muck-analyze/v1.0.0/warn/frequently/queryPage")
	) {
		copyFilePath = `/inneral/frequently/index`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/warn/frequently/historyWarning"
		)
	) {
		copyFilePath = `/inneral/frequently/historyWarning`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/electron/fence/warning/getCarTeamByLicensePlateNumber?licensePlateNumber"
		)
	) {
		copyFilePath = `/inneral/frequently/getCarTeamByLicensePlateNumber`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/trail/selectTrail?pageNum=1&pageSize=20"
		)
	) {
		copyFilePath = `/inneral/carPath/index`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/trail/getMuckTrailList?startTime=2022-09-25"
		)
	) {
		copyFilePath = `/inneral/carPath/getMuckTrailList`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/trail/muckTrailBatchRecord?startTime=2022-09-25"
		)
	) {
		copyFilePath = `/inneral/carPath/muckTrailBatchRecord`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/trail/statisticsMuckEventCount?startTime=2022-09-25"
		)
	) {
		copyFilePath = `/inneral/carPath/statisticsMuckEventCount`;
	}
	if (
		filePath.includes(
			"/inneral-muck-analyze/v1.0.0/muck/trail/getMuckCarHotList"
		)
	) {
		if (filePath.includes("licensePlateNumber=%E9%99%95A32SG3")) {
			copyFilePath = `/inneral/carPath/2`;
		} else {
			copyFilePath = `/inneral/carPath/getMuckCarHotList`;
		}
	}
	return copyFilePath;
};
